﻿Imports Infragistics.Win.UltraWinGrid
Imports Crystal.CommonDBManagement.BLL
Imports Crystal.CLL

Public Class frmDeleteLKPTable
#Region "Declartion"
    Private _datatable As DataTable
    Private _tableID As Integer
    Private _deleteRecords As DataTable
    Private _pendingRecords As DataTable
    Public Event deleteRecord(ByVal dt As DataTable, ByVal isDeleted As Boolean)

#End Region
#Region "Properties"
    Public Property Datatable() As DataTable
        Get
            Return _datatable
        End Get
        Set(ByVal value As DataTable)
            _datatable = value
        End Set
    End Property

    Public Property TableID() As Integer
        Get
            Return _tableID
        End Get
        Set(ByVal value As Integer)
            _tableID = value
        End Set
    End Property
#End Region

    Private Sub frmDeleteLKPTable_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Text = TitleManager.GetTitle("frmDeleteLKPTable")
        grdRecords.DataSource = Datatable
        If grdRecords.Rows.Count > 0 Then
            grdRecords.Rows(0).Activation = Activation.ActivateOnly
        End If
        HideGridColumns()
        'FormatGrid(grdFunds)
        'lblmessage.Text = String.Empty
        Me.grdRecords.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
    End Sub

    Public Sub HideGridColumns()
        Try
            If grdRecords.Rows.Count > 0 Then
                grdRecords.DisplayLayout.Bands(0).Columns(0).Hidden = True
                If grdRecords.DisplayLayout.Bands(0).Columns.Exists("IsDefault") Then
                    grdRecords.DisplayLayout.Bands(0).Columns("IsDefault").Hidden = True
                End If
                If grdRecords.DisplayLayout.Bands(0).Columns.Exists("Delete") Then
                    grdRecords.DisplayLayout.Bands(0).Columns("Delete").Hidden = True
                End If
                For Each col As UltraGridColumn In grdRecords.DisplayLayout.Bands(0).Columns
                    If col.Header.Caption.Contains("ID") Then
                        col.Hidden = True
                    End If
                Next
                If grdRecords.DisplayLayout.Bands(0).Columns.Exists("IsExcludedFromSearch") Then
                    grdRecords.DisplayLayout.Bands(0).Columns("IsExcludedFromSearch").Hidden = True
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim objReferenceTableInfo As New cAddEditLKPTable
        Dim isDeleted As Boolean = False
        _pendingRecords = New DataTable()
        _pendingRecords = Datatable.Clone()
        _deleteRecords = New DataTable()
        _deleteRecords = Datatable.Clone()
        Try
            If btnCancel.Enabled = False Then
                If MessageManager.GetMessage("RECORDS_CNT_DELETE") = lblInform.Text Then
                    isDeleted = True
                    Exit Sub
                End If
            End If
            For Each row As DataRow In Datatable.Rows
                'If row.Item(0).ToString = String.Empty Then
                '    _deleteRecords.ImportRow(row)
                '    Continue For
                'End If
                If (objReferenceTableInfo.CheckBeforeDelete(TableID, row.Item(0)) = True) Then
                    If objReferenceTableInfo.DeleteReferenceTableData(TableID, row.Item(0)) = True Then
                        _deleteRecords.ImportRow(row)
                    Else
                        _pendingRecords.ImportRow(row)
                    End If
                Else
                    _pendingRecords.ImportRow(row)
                End If
            Next
            If Datatable.Rows.Count = _deleteRecords.Rows.Count Then
                ShowMessageBox(DBResult.RECORDS_DELETED)
                RaiseEvent deleteRecord(_deleteRecords, True)
                isDeleted = True
            Else
                If _pendingRecords.Rows.Count > 0 Then
                    If _deleteRecords.Rows.Count > 0 Then
                        RaiseEvent deleteRecord(_deleteRecords, True)
                    End If
                    grdRecords.DataSource = _pendingRecords
                    lblInform.Text = MessageManager.GetMessage("RECORDS_CNT_DELETE")
                    btnCancel.Enabled = False
                End If
            End If
        Catch ex As Exception
            Throw ex
        Finally
            If isDeleted Then
                isDeleted = False
                Me.Close()
            End If
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
End Class